//
//  GuideViewController.m
//  ios-loan
//
//  Created by jpush on 2018/1/3.
//  Copyright © 2018年 hxhg. All rights reserved.
//

#import "GuideViewController.h"
#import "ALMacro.h"
#import "ALUtilsMacro.h"
#import "ADScreenAdaptation.h"
#import "PureLayout.h"
#import "UIFont+category.h"

#define GUIDE_MAX_PAGES 4
typedef void(^GuideCompletion)(void);

@interface GuideViewController () <UIScrollViewDelegate>
@property (nonatomic ,assign) BOOL            skipForGuide;
@property (nonatomic ,assign) GuideViewStyle  style;
@property (nonatomic ,strong) UIScrollView    *scrollView;
@property (nonatomic ,strong) UIPageControl   *pageControl;
@property (nonatomic ,strong) UIButton        *enterButton;
@property (nonatomic ,copy) GuideCompletion completion;
@property (nonatomic ,copy) GuideCompletion exit;
@end

@implementation GuideViewController

- (instancetype)initWithSkipForGuide:(BOOL)skipForGuide style:(GuideViewStyle)style completion:(void (^)(void))completion exit:(void(^)(void))exit{
    self=[super init];
    if (self) {
        self.skipForGuide=skipForGuide;
        self.style=style;
        self.completion=completion;
        self.exit = exit;
    }
    return self;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    self.automaticallyAdjustsScrollViewInsets=NO;
    self.view.backgroundColor=[UIColor clearColor];
    self.scrollView=[[UIScrollView alloc]initWithFrame:self.view.bounds];
    self.scrollView.contentSize=CGSizeMake(CGRectGetWidth(self.scrollView.bounds)*(GUIDE_MAX_PAGES), CGRectGetHeight(self.scrollView.bounds));
    self.scrollView.showsHorizontalScrollIndicator=NO;
    self.scrollView.pagingEnabled=YES;
    self.scrollView.backgroundColor=[UIColor clearColor];
    self.scrollView.bounces=NO;
    self.scrollView.delegate=self;
    [self.view addSubview:self.scrollView];
    CGFloat y=56;
    if (kDevice_Is_iPhoneX) {
        self.pageControl=[[UIPageControl alloc]
                          initWithFrame:CGRectMake(0, CGRectGetHeight(self.view.frame)-AL_IphonexTabbarVirtualHeight-3.5-adsaH(20), 200,3.5)];
    }else{
        self.pageControl=[[UIPageControl alloc]
                          initWithFrame:CGRectMake(0, CGRectGetHeight(self.view.frame)-3.5-adsaH(20), 200,3.5)];
    }
    self.pageControl.center=CGPointMake(CGRectGetWidth(self.view.frame)/2.0, self.pageControl.center.y);
    self.pageControl.numberOfPages=GUIDE_MAX_PAGES;
    self.pageControl.currentPage=0;
    self.pageControl.currentPageIndicatorTintColor = RGB(58, 139, 255);
    self.pageControl.pageIndicatorTintColor = RGB(215, 233, 254);
    
    [self.view addSubview:self.pageControl];
    UIView *loginView=nil;
    for(int j=1;j<=GUIDE_MAX_PAGES;j++){
        NSString *imageName=[NSString stringWithFormat:@"Launch_Guide_%d_%d_%d",j,(int)kSCREEN_WIDTH,(int)kSCREEN_HEIGHT];
        UIImage *image=kImageWithName(imageName);
        if (!image) {
            imageName=[NSString stringWithFormat:@"Launch_Guide_%d_%d_%d",j,375,667];
            image=kImageWithName(imageName);
        }
        UIImageView *imageView=[[UIImageView alloc]initWithImage:image];
        imageView.frame=CGRectMake(CGRectGetWidth(self.scrollView.frame)*(j-1), 0, CGRectGetWidth(self.scrollView.frame), CGRectGetHeight(self.scrollView.frame));
        [self.scrollView addSubview:imageView];
        if (j==GUIDE_MAX_PAGES) {
            loginView=imageView;
            imageView.userInteractionEnabled=YES;
            [imageView addSubview:self.enterButton];
        }
    }

    [self.enterButton autoSetDimensionsToSize:CGSizeMake(adsaW(300/2), adsaW(80/2))];
    if (kDevice_Is_iPhoneX){
        [self.enterButton autoPinEdgeToSuperviewEdge:ALEdgeBottom withInset:(adsaH(70)+AL_IphonexTabbarVirtualHeight)];
    }else{
        [self.enterButton autoPinEdgeToSuperviewEdge:ALEdgeBottom withInset:adsaH(70)];
    }
    [self.enterButton autoAlignAxisToSuperviewAxis:ALAxisVertical];
    
    if (self.skipForGuide) {
        self.scrollView.contentOffset=CGPointMake(CGRectGetWidth(self.view.frame)*(GUIDE_MAX_PAGES-1), 0);
        [UIApplication sharedApplication].statusBarStyle=UIStatusBarStyleLightContent;
    }else{
        [UIApplication sharedApplication].statusBarStyle=UIStatusBarStyleDefault;
    }
}
- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
//    [self.navigationController setNavigationBarHidden:YES animated:NO];
}

- (void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:animated];
}
- (void)viewDidAppear:(BOOL)animated{
    [super viewDidAppear:animated];
    if (self.completion) {
        self.completion();
        self.completion=nil;
    }
//    [JANALYTICSService xj_startLogPageView:@"page_guide"];
}

- (void)viewDidDisappear:(BOOL)animated {
    [super viewDidDisappear:animated];
//    [JANALYTICSService xj_stopLogPageView:@"page_guide"];
}

- (void)scrollViewDidScroll:(UIScrollView *)scrollView{
    int i=(int)((scrollView.contentOffset.x/scrollView.frame.size.width)+0.5);
    int expectPage=MIN(i, GUIDE_MAX_PAGES);
    if (self.pageControl.currentPage!=expectPage) {
        self.pageControl.currentPage=expectPage;
        [self setNeedsStatusBarAppearanceUpdate];
    }
//    if (scrollView.contentOffset.x>=scrollView.contentSize.width-CGRectGetWidth(scrollView.frame)-CGRectGetWidth(scrollView.frame)/2.0) {
//        self.pageControl.hidden=YES;
//    }else{
//        self.pageControl.hidden=NO;
//    }
//    if(scrollView.contentOffset.x>=scrollView.contentSize.width-CGRectGetWidth(scrollView.frame)){
//        scrollView.scrollEnabled=NO;
//    }
}

- (void)enter{
    if (self.exit) {
        self.exit();
    }
}
#pragma mark --
#pragma mark -- setter

- (void)setIsStatusBarBlack:(BOOL)isStatusBarBlack{
    _isStatusBarBlack=isStatusBarBlack;
    [self setNeedsStatusBarAppearanceUpdate];
}
#pragma mark --
#pragma mark --  getter

- (UIButton*)enterButton{
    if (!_enterButton) {
        _enterButton=[UIButton buttonWithType:UIButtonTypeCustom];
        _enterButton.backgroundColor=[UIColor whiteColor];
        [_enterButton setTitle:@"立即体验" forState:UIControlStateNormal];
        [_enterButton setTitleColor:RGB(58,139,255) forState:UIControlStateNormal];
        [_enterButton addTarget:self action:@selector(enter) forControlEvents:UIControlEventTouchUpInside];
        _enterButton.titleLabel.font = [UIFont scale_boldSystemFontOfSize:38/2];
        _enterButton.adjustsImageWhenHighlighted=NO;
        _enterButton.layer.cornerRadius = adsaW(5);
        _enterButton.layer.borderColor=RGB(58,139,255).CGColor;
        _enterButton.layer.borderWidth=1.0f;
        _enterButton.clipsToBounds = YES;
        [_enterButton setTitleEdgeInsets:UIEdgeInsetsMake(14/2, 42/2, 14/2, 42/2)];
    }
    return _enterButton;
}
#pragma mark --
#pragma mark -- status bar
- (UIStatusBarStyle)preferredStatusBarStyle{
    if (self.isStatusBarBlack) {
        return UIStatusBarStyleDefault;
    }
    if (self.pageControl.currentPage<GUIDE_MAX_PAGES-1) {
        return UIStatusBarStyleDefault;
    }else{
        return UIStatusBarStyleDefault;
    }
}
- (UIStatusBarAnimation)preferredStatusBarUpdateAnimation{
    return UIStatusBarAnimationNone;
}

@end
